package com.chunuo.commonbase.exception;

import com.chunuo.commonutils.R;
import io.jsonwebtoken.ExpiredJwtException;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

/**
 * 类的作用
 *
 * @author liujiamiao
 * @email 2544296058@qq.com
 * @date 2020-07-04 11:44
 */
@RestControllerAdvice
@Slf4j
public class MyExceptionHandler {

    /**
     * 统一异常
     * @param e
     * @return
     */
    @ExceptionHandler(Exception.class)
    public R error(Exception e){
        System.out.println("异常...");
        log.error("异常："+e.getMessage());
        e.printStackTrace();
        return R.error().data("message","异常统一处理");
    }

    /**
     * 自定义指定异常
     * @param e
     * @return
     */
    @ExceptionHandler(GuliException.class)
    public R guliException(GuliException e){
        log.error("异常："+e.getMessage()+" === "+e.getMsg());
        e.printStackTrace();
        return R.error().data("code",e.getCode()).data("message",e.getMsg());
    }

    /**
     * 登录过时
     * @param e
     * @return
     */
    @ExceptionHandler(ExpiredJwtException.class)
    public R outItemException(ExpiredJwtException e){
        log.error("异常："+e.getMessage());
        e.printStackTrace();
        return R.errorCustom().code(50014).message("登录过时，请重新登录");
    }

}
